package com.iot.common.utils;

public class ByteToUnsignedNumberUtils {

	/**
	 * byte转int
	 * 
	 * @param b
	 * @return
	 */
	public static int byte1ToUInt(byte b) {
		return (int)b /*& 0x0FF*/;
	}

	/**
	 * 将2字节的byte转换为带符号的short
	 * 
	 * @param b
	 * @return
	 */
	private static short byte2ToShort(byte[] b) {
		short s = 0;
		short s1 = (short) (b[0] & 0xff);// 最低位
		short s0 = (short) (b[1] & 0xff);
		s1 <<= 8;
		s = (short) (s0 | s1);
		return s;
	}

	/**
	 * 将2字节的byte转换为无符号的int
	 * 
	 * @param b
	 * @return
	 */
	public static int byte2ToUInt(byte[] b) {
		return UnsignedNumberUtils.toUnsignedShortValue(byte2ToShort(b));
	}

	/**
	 * 将4字节的byte转换为带符号的int
	 * 
	 * @param b
	 * @return
	 */
	private static int byte4ToInt(byte[] b) {
		int s = 0;
		int s3 = b[0] & 0xff;// 最低位
		int s2 = b[1] & 0xff;
		int s1 = b[2] & 0xff;
		int s0 = b[3] & 0xff;
		s3 <<= 24;
		s2 <<= 16;
		s1 <<= 8;
		s = s0 | s1 | s2 | s3;
		return s;
	}

	/**
	 * 将4字节的byte转换为无符号的long
	 * 
	 * @param b
	 * @return
	 */
	public static long byte4ToULong(byte[] b) {
		return UnsignedNumberUtils.toUnsignedIntValue(byte4ToInt(b));
	}

	/**
	 * int转byte,int需要小于256
	 * 
	 * @param i
	 * @return
	 */
	public static byte intToByte1(int i) {
		return (byte) i;
	}

	/**
	 * int转 4字节
	 * 
	 * @param number
	 * @return
	 */
	private static byte[] intToByte4(int number) {
		byte[] b =new byte[4];
		b[3]=(byte)(number&0xff);
		b[2]=(byte)(number>>8&0xff);
		b[1]=(byte)(number>>16&0xff);
		b[0]=(byte)(number>>24&0xff);
		return b;
	}

	/**
	 * 将带符号的int转换为无符号的long
	 * 
	 * @param i
	 * @return
	 */
	public static long intToULong(int i) {
		return UnsignedNumberUtils.toUnsignedIntValue(i);
	}

	/**
	 * long类型转成8字节数组
	 * 
	 * @param number
	 * @return
	 */
	public static byte[] longToByte8(long number) {
		long temp = number;
		byte[] b = new byte[8];
		for (int i = 0; i < b.length; i++) {
			b[i] = new Long(temp & 0xff).byteValue();// 将最低位保存在最低位
			temp = temp >> 8; // 向右移8位
		}
		return b;
	}

	public static void main(String[] args) {

		int min = Integer.MIN_VALUE;
		int max = Integer.MAX_VALUE;
		System.out.println("imin:" + min);
		System.out.println("imax:" + max);
		int ttt = 2143283144;
		byte[] bttt = intToByte4(ttt);
		long lttt = byte4ToULong(bttt);
		System.out.println(lttt);

		System.out.println("__________________");
		short smin = Short.MIN_VALUE;
		short smax = Short.MAX_VALUE;
		System.out.println("smin:" + smin);
		System.out.println("smax:" + smax);
		short stttt = -100;
		byte[] stemp = shortToByte2(stttt);
		int itttt = byte2ToUInt(stemp);
		System.out.println("" + itttt);
		System.out.println("__________________");
		long ll=20170503134235L;
		System.out.println("LL>"+ll);
		byte[] lltest=uintToByte4(ll);
		ll=byte4ToULong(lltest);
		System.out.println("LL>"+ll);
		
		String bbb="20170503142634";
		byte[] bbbb=bbb.getBytes();
		System.out.println(bbbb.length+"");
	}

	/**
	 * short转2字节
	 * 
	 * @param number
	 * @return
	 */
	private static byte[] shortToByte2(short number) {
//		int temp = number;
		byte[] b = new byte[2];
//		for (int i = 0; i < b.length; i++) {
//			b[i] = new Integer(temp & 0xff).byteValue();// 将最低位保存在最低位
//			temp = temp >> 8; // 向右移8位
//		}
		b[0]=(byte)(number>>8&0xff);
		b[1]=(byte)(number&0xff);
		return b;
	}

	/**
	 * 将带符号的short转换为无符号的int
	 * 
	 * @param s
	 * @return
	 */
	public static int shortToUInt(Short s) {
		return UnsignedNumberUtils.toUnsignedShortValue(s.shortValue());
	}

	/**
	 * 将无符号位int转换为4字节数组，输入实际为long
	 * 
	 * @param number
	 * @return
	 */
	public static byte[] uintToByte4(long number) {
		long temp = 0;
		if (number > Integer.MAX_VALUE) {
			temp = (int) (number - Integer.MAX_VALUE);
		} else {
			temp = (int) number;
		}
//		temp=Long.reverseBytes(temp);
		return intToByte4((int)temp);
	}

	/**
	 * 将无符号位short转换为2字节数组，输入实际为int
	 * 
	 * @param number
	 * @return
	 */
	public static byte[] ushortToByte2(int number) {
		short temp = 0;
		if (number > Short.MAX_VALUE) {
			temp = (short) (number - Short.MAX_VALUE);
		} else {
			temp = (short) number;
		}
		return shortToByte2(temp);
	}
}
